<template>
  <div class="w h over-h">
    <div class="category-title a-i-c f-s dialog-page-title">
      <el-button type='text' icon="ArrowLeft" @click="goBackToPage">返回</el-button>
      <div class="m-l10">
        {{ params.isEdit ? '团队编辑' : '团队新增' }}
      </div>
    </div>
    <div class="w f-c a-i-c" :style="normalBodyHeight">
      <div style="width:600px">
        <lm-form :ref="editFormRefKey" inline label-width="120px" class="lm-w lm-h" :layout="editFromLayout"
          v-model="editForm">
          <template #meta>
            <span class="c7">
              Meta:{{ metaString }}
            </span>
          </template>
        </lm-form>
        <div class="lm-w lm-h lm-f-s">
          <div class="lm-w-120 lm-h"></div>
          <el-button type="primary" @click="submit">提交</el-button>
          <el-button type="danger" @click="reset" plain>重置</el-button>
          <el-button type="primary" @click="goBackToPage()" plain>返回</el-button>
        </div>
      </div>
    </div>
  </div>
</template>

<script lang="ts">
import frameProperty from '@/mixins/frame-property'
import { Team, type TeamInfo } from '@/apis/team/index'
import { RequestCode } from '@/server/base'
import { ValidateFieldsError } from 'async-validator'

export default {
  name: 'TeamEdit',
  mixins: [frameProperty],
  props: {
    goBackToPage: Function,
    params: [Object, Array]
  } as any,
  data() {
    return {
      editFormRefKey: 'teamModifyRef',

      editForm: {} as {} | TeamInfo | any,
      editFromLayout: [
        {
          label: '团队名称',
          prop: 'name',
          type: 'input',
          itemClass: 'column_2',
          message: '请输入2-10个字符',
          required: true,
          clearable: true
        },
        {
          prop: 'type',
          label: '类型',
          type: 'select',
          itemClass: 'column_2',
          clearable: true,
          required: true,
          options: [
            { label: '标注', value: '0' },
            { label: '审批', value: '1' },
            { label: '清洗', value: '2' }
          ]
        },
        {
          prop: 'explain',
          label: '描述',
          type: 'textarea',
          itemClass: 'column_1',
          rows: 6,
          message: '请输入2-100个字符',
          required: true,
          clearable: true,
          regex: /^.{2,100}$/
        }
      ] as any[]
    }
  },
  computed: {
    modifyInfo() {
      return this.params.row
    }
  },
  methods: {
    getDetailById() {
      if (!this.modifyInfo) {
        return
      }
      this.editForm = this.modifyInfo
      if (this.editForm.type != undefined) {
        this.editForm.type = this.modifyInfo.type + ''
      }
    },
    saveTeamInfo() {
      const method = this.params.isEdit ? Team.modifyTeamInfo : Team.addTeamInfo
      method(this.editForm)
        .then((res: RequestCode) => {
          this.showMessage({ type: 'success', message: '操作成功！' })
          this.goBackToPage(true)
        })
        .catch((error: any) => { })
    },
    //验证数据
    submit() {
      const editFormRef = this.$refs[this.editFormRefKey] as any
      editFormRef.validate((isValid: boolean, invalidFields?: ValidateFieldsError) => {
        if (!isValid) {
          //滚动到未通过校验的字段
          if (invalidFields && Object.keys(invalidFields).length > 0) {
            editFormRef.scrollToField(Object.keys(invalidFields)[0])
          }
          return
        }
        this.saveTeamInfo()
      })
    },
    //重置
    reset() {
      this.editForm = {}
    }
  },
  created() {
    this.$nextTick(() => {
      this.getDetailById()
      // this.getDataList()
    })
  }
}
</script>

<style scoped></style> 